// pages/freshLife/freshLife.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.dialog = this.selectComponent("#dialog");
    this.dialog.showDialog();
    //获取手机高度
    this.setData({
      navH: app.globalData.navHeight,
      type: options.type,
    });
    console.info(this.data.type);
    that.getAllCookBookList();
    
  },

  //获取直播列表
  getLiveInfo:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + "getLiveInfo",
      data: {},
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          liveInfo: res.data.data.room_info
        })
      },
    })
  },

  //获取所有食谱列表
  getAllCookBookList:function(){
    var that = this;
    wx.request({
      url: app.globalData.url + 'getCookBookList?productId=0',
      method:"get",
      success:function(res){
        console.log(res.data);
        that.setData({
          cookBookList:res.data.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAllDryingList();
    this.getLiveInfo();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // },

  click: function (e) {
    var that = this;
    var type = e.currentTarget.dataset.type;
    if (that.data.type == type) {
      return fasle;
    } else {
      that.setData({
        type: type
      })
    }
    if (type == 2) {
      that.getLiveInfo();
    }
  },

  //获取所有客户晒单
  getAllDryingList: function () {
    var that = this;
    wx.request({
      url: app.globalData.url + 'getDryingList',
      method: "get",
      success: function (res) {
        if (res.data.result ==1 ) {
          var list = res.data.data;
          var imageList = [];
          var images = [];
          var strs = '';
          console.log(list);
          for (var i = 0; i < list.length; i++) {
            if (list[i].reviewImages != null || list[i].reviewImages + '' != 'undefined') {
              strs = list[i].reviewImages;
              images = strs.split(";;;");
              imageList.push(images);
            } else {
              imageList.push([]);
            }
          }
          that.setData({
            dryingList: list.reverse(),
            imageList: imageList,
          })
        }
      }
    })
  },

  //跳转食谱详情
  gorecipeDetail:function(e){
    var that = this;
    var index = e.currentTarget.dataset.index;
    var cookJson = JSON.stringify(that.data.cookBookList[index]);
    wx.navigateTo({
      url: '/pages/recipeDetail/recipeDetail?cookId=' + e.currentTarget.dataset.cook ,
    })
  },

  //跳转美食详情
  goFoodDetail:function(e){
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/webView/webView?url=' + app.globalData.url + 'getWebPage&webId=' + id,
    });
  },

  //添加到购物车
  addcart:function(e){
    var that = this;
    var productId = e.currentTarget.dataset.productid;
    wx.request({
      url: app.globalData.url + 'addProductToShoppingCart',
      data:{
        userId:wx.getStorageSync("userId"),
        productId:e.currentTarget.dataset.productid,
        buyCount:1,
      },
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method:"post",
      success:function(res){
        if(res.data.result == 1){
          wx.showToast({
            title: '添加成功！',
            icon:"success",
          })
        } else {
          wx.showToast({
            title: res.data.errmsg,
            icon: "none",
          })
        }
      }
    })
  },

  //跳转商品详情页
  toProductDetail:function(e){
    wx.navigateTo({
      url: '/pages/productDetail/productDetail?productid=' + e.currentTarget.dataset.productid,
    })
  },

  navBack: function () {
    wx.navigateBack({

    })
  },
  toLive: function (e) {
    var roomid = e.currentTarget.dataset.roomid;
    wx.navigateTo({
      url: 'plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=' + roomid + '&custom_params'
    })
  },
})